﻿using PeshoMVC.Models;
using PeshoMVC.MyHelpers;
using System;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;

namespace PeshoMVC.Controllers
{
    [Authorize]
    public class UserController : Controller
    {
        public ActionResult Index()
        {
            var model = Data.GetUser(User.Identity.Name);
            return View(model);
        }

        public ActionResult Edit(Guid? id)
        {
            if (id != null)
            {
                var model = Data.GetUser(id);
                return View(model);
            }

            return View();
        }

        [HttpPost]
        public ActionResult Edit(UserProfileModel model)
        {
            if (model.Day != 0 && model.Mount != 0)
            {
                model.Birthday = new DateTime(model.Year, model.Mount, model.Day);
            }

            Data.SaveData(model);

            return RedirectToAction("Index");
        }

        public ActionResult ChangeMail()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangeMail(ChangeUserEmailModel model)
        {
            var user = Membership.GetUser(User.Identity.Name);

            if (user.Email == model.OldEmail)
            {
                user.Email = model.NewEmail;
                Membership.UpdateUser(user);

                return View("Index");
            }
            else
            {
                ViewBag.Error = "This is not your original email!";
            }

            return View();
        }

        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangeUserPasswordModel model)
        {
            if (ModelState.IsValid)
            {
                var user = Membership.ValidateUser(User.Identity.Name, model.OldPassword);

                if (user)
                {
                    var curUser = Membership.GetUser(User.Identity.Name);

                    curUser.ChangePassword(model.OldPassword, model.NewPassword);
                    Membership.UpdateUser(curUser);

                    return View("Index");
                }
                else
                {
                    ViewBag.Error = "This is not your original password!";
                }
            }

            return View();
        }

        public ActionResult GetImage(string name)
        {
            var model = Data.GetUser(name);

            return File(model.Picture, "image/jpg");
        }

        public ActionResult ChangePicture(string name)
        {
            var model = Data.GetUser(name);

            return View(model);
        }

        [HttpPost]
        public ActionResult ChangePicture(UserProfileModel model)
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0 && (file.ContentType == "image/png" || file.ContentType == "image/jpeg"))
                {
                    string filename = file.FileName;
                    byte[] image = new byte[file.ContentLength];
                    Stream fileStream = file.InputStream;
                    fileStream.Read(image, 0, file.ContentLength);

                    DataClassesDataContext db = new DataClassesDataContext();

                    var bla =
                        from u in db.UsersInformations
                        where u.UserId == model.UserId
                        select u;

                    foreach (UsersInformation item in bla)
                    {
                        item.Photo = image;
                    }

                    db.SubmitChanges();

                    ViewBag.Message = "Upload status: File uploaded!";
                }
                else
                {
                    ViewBag.Error = "Upload status: Please, select a valid image!";
                }
            }
            else
            {
                ViewBag.Error = "Upload status: Please, selected some file!";
            }

            return View(model);
        }
    }
}
